# Maintainer: Martell Malone <martellmalone@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=vlc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=3.0.18
pkgrel=11
pkgdesc="A multi-platform MPEG, VCD/DVD, and DivX player (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.videolan.org/vlc/"
install=vlc-${MSYSTEM}.install
license=('spdx:LGPL-2.1-or-later OR spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-a52dec"
         "${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-aribb24"
         "${MINGW_PACKAGE_PREFIX}-chromaprint"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-faad2"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-fluidsynth"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gsm"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbluray"
         "${MINGW_PACKAGE_PREFIX}-libcaca"
         "${MINGW_PACKAGE_PREFIX}-libcddb"
         "${MINGW_PACKAGE_PREFIX}-libcdio"
         "${MINGW_PACKAGE_PREFIX}-libdca"
         "${MINGW_PACKAGE_PREFIX}-libdsm"
         "${MINGW_PACKAGE_PREFIX}-libdvdcss"
         "${MINGW_PACKAGE_PREFIX}-libdvdnav"
         "${MINGW_PACKAGE_PREFIX}-libdvbpsi"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-libgme"
         "${MINGW_PACKAGE_PREFIX}-libgoom2"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libmad"
         "${MINGW_PACKAGE_PREFIX}-libmatroska"
         "${MINGW_PACKAGE_PREFIX}-libmfx"
         "${MINGW_PACKAGE_PREFIX}-libmicrodns"
         "${MINGW_PACKAGE_PREFIX}-libmodplug"
         "${MINGW_PACKAGE_PREFIX}-libmpcdec"
         "${MINGW_PACKAGE_PREFIX}-libmpeg2"
         "${MINGW_PACKAGE_PREFIX}-libmysofa"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libnfs"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libnotify"
         "${MINGW_PACKAGE_PREFIX}-libplacebo"
         "${MINGW_PACKAGE_PREFIX}-libproxy"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libsamplerate"
         "${MINGW_PACKAGE_PREFIX}-libsecret"
         "${MINGW_PACKAGE_PREFIX}-libshout"
         "${MINGW_PACKAGE_PREFIX}-libsoxr"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvncserver"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lua51"
         "${MINGW_PACKAGE_PREFIX}-minizip"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-portaudio"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-pupnp"
         "${MINGW_PACKAGE_PREFIX}-schroedinger"
         "${MINGW_PACKAGE_PREFIX}-speex"
         "${MINGW_PACKAGE_PREFIX}-srt"
         "${MINGW_PACKAGE_PREFIX}-taglib"
         "${MINGW_PACKAGE_PREFIX}-twolame"
         "${MINGW_PACKAGE_PREFIX}-vcdimager"
         "${MINGW_PACKAGE_PREFIX}-libx264"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-SDL_image"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-glew"
             "${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=(https://get.videolan.org/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.xz
        0001-Use-libdir-for-plugins-on-msys2.patch
        0002-MinGW-w64-lfind-s-_NumOfElements-is-an-unsigned-int.patch
        0003-MinGW-w64-don-t-pass-static-to-pkg-config-if-SYS-min.patch
        0004-Revert-Win32-prefer-the-static-libraries-when-creati.patch
        0005-Remove-legacy-defines.patch
        0006-Linking-libqt_plugin-with-winmm.patch
        0007-Mingw-load-libraries-not-only-from-system32.patch
        0019-qt5-headers.patch
        0023-opencv4-support.patch
        0025-iovec-redefined.patch
        0026-fix-libsynchronization-check.patch
        https://code.videolan.org/videolan/vlc/-/commit/65ea8d19d91ac1599a29e8411485a72fe89c45e2.patch)
sha256sums=('57094439c365d8aa8b9b41fa3080cc0eef2befe6025bb5cef722accc625aedec'
            '130cdf7655deca92fd28dd177cd99aab24113bd1052700896046b66acbcb1f2d'
            '8f40d57bb087306e5099c5b9067dacca0479c4dc9e45eb902ad04eeb3fb83ef3'
            '072de5cda7772a63d81a067be1b942e1cf59f6b53206e61d0caac9dc18216ad6'
            '416f6c11eb8a47ea3d0f258fa7d471e0eb53b79e4816913d083aacead0113e7a'
            '612b7718c76b23ed0f69ed28b06d3b67ae89b1764b62dc7443e9edc26a541e2e'
            '71eada1e1a93e77f7a6c76423e4d39e9f8f700ca12322c3ffa7ecfaa3debf5ae'
            '194b85e50f2c410ce50d7c7ff23bc915020c9c073a83f64f12c8247d41e1b475'
            '278e6111e467dfb1b6b4228489dc6ab029abfa11df3c77d8cd8021987a11b4ea'
            'b7651185a3c78c4bbfd3ef92b9af0d894cc6276754447d9fd705c46df7a33526'
            '2b0f45a93c804e8e6e59a56d0ed82d2bc703a3b7ef166aa98ad311d9b15d4f08'
            '54ddd07473da265ebaff7d928a23bcc639354513d5e0677afe21b6496f934625'
            '695f984f5974343fd44a8208a7170f85d2253ffd3a276171556e2e02b4bc8fcf')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-Use-libdir-for-plugins-on-msys2.patch \
    0002-MinGW-w64-lfind-s-_NumOfElements-is-an-unsigned-int.patch \
    0003-MinGW-w64-don-t-pass-static-to-pkg-config-if-SYS-min.patch \
    0004-Revert-Win32-prefer-the-static-libraries-when-creati.patch \
    0005-Remove-legacy-defines.patch \
    0006-Linking-libqt_plugin-with-winmm.patch \
    0007-Mingw-load-libraries-not-only-from-system32.patch \
    0019-qt5-headers.patch \
    0025-iovec-redefined.patch \
    0026-fix-libsynchronization-check.patch \
    65ea8d19d91ac1599a29e8411485a72fe89c45e2.patch

  ./bootstrap
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  # Hack getting private QtGui directory
  local _qtver=$(qmake -query QT_VERSION)
  local _qtguiinc=$(pkg-config Qt5Gui --variable=includedir)
  local _privdir=${_qtguiinc}/QtGui/${_qtver}/QtGui
  CPPFLAGS+=" -I${_privdir}"
  # Too many warnings
  CFLAGS+=" -w"
  CXXFLAGS+=" -w"

  BUILDCC="${CC}" \
  ${srcdir}/${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-binary-version="msys2" \
    --enable-qt \
    --enable-dav1d \
    --disable-ncurses \
    --disable-dbus \
    --disable-telx \
    --enable-nls \
    --disable-lua \
    --disable-gst-decode

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR="${pkgdir}" install

  for res in 16 32 48 128; do
    install -D -m644 "${srcdir}/${_realname}-${pkgver}/share/icons/${res}x${res}/vlc.png" \
                     "${pkgdir}/${MINGW_PREFIX}/share/icons/hicolor/${res}x${res}/apps/vlc.png"
  done
}
